
var app = new Vue({
    el: '.box',
    data: {
        options: '',
        value: '',
        formLabelWidth: '70px',
        dialogFormVisible: false,
        up_img: '',//封面图片
        md: '',//md数据
        html: '',//html数据
        tag: '',//分类
        title: '',//标题
        ids: '',//编辑标题
    },
    methods: {
        select_img() {
            // var select = document.querySelector('#select');
            var photo = document.querySelector('#photo');
            photo.click();
            photo.addEventListener('change', function () {
                if (photo.files[0] || photo) {
                    app.up_img = window.URL.createObjectURL(photo.files[0]);
                };//预览图片
            }, false);
        },
        upload_run() {
            upload_img_fun();
        },
        save_book() {
            var md = testEditor.getMarkdown();//获取md格式数据
            var html = testEditor.getHTML();//获取html格式数据
            if (!md || !html) {
                this.$message.error('请输入内容哦~');
                return false;
            };
            if (this.tag == '') {
                this.$message.error('请选择分类哦~');
                return false;
            };
            if (this.title == '') {
                this.$message.error('请输入标题哦~');
                return false;
            };
            if (this.up_img.includes('blob:')) {
                this.$message.error('请上传图片哦~');
                return false;
            };

            if (md.includes('+')) {
                md = md.replace(/\+/g, "%2B");
                html = html.replace(/\+/g, "%2B");
            };
            if (md.includes("'")) {
                md = md.replace(/\'/g, "&#x27;");
                html = html.replace(/\'/g, "&#x27;");
            };
            if (html.includes("'")) {
                md = md.replace(/\'/g, "&#x27;");
                html = html.replace(/\'/g, "&#x27;");
            };
            if (html.includes("&")) {
                md = md.replace(/\&/g, "%26");
                html = html.replace(/\&/g, "%26");
            };
            if (md.includes("&")) {
                md = md.replace(/\&/g, "%26");
                html = html.replace(/\&/g, "%26");
            };
            if (html.includes("<")) {
                md = md.replace(/\</g, "%3C");
                html = html.replace(/\</g, "%3C");
            };
            if (html.includes(">")) {
                md = md.replace(/\>/g, "%3E");
                html = html.replace(/\>/g, "%3E");
            };

            var arr = { flg: 1, head: 1024, token: (sessionStorage.getItem('token')), title: this.title, md: md, html: html, tag: this.tag, up_img: this.up_img };
            if (!this.ids) {
                save_book(arr);
            } else {
                arr.head = 1026;
                arr.flg = 0;
                arr.id = this.ids;
                save_book(arr);
                //保存修改
            };//
        }
    }
});

function new_book(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    obj.head = 20000;
    console.log(obj);
    axios({
        method: "POST",
        baseURL: '../../api/new_file_book.php',
        data: 'data=' + JSON.stringify(obj)
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            console.log(res);
            setTimeout(function () { location.reload(); }, 1000);
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//保存文章


function save_book(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",
        baseURL: '../../api/book.php',
        data: 'data=' + JSON.stringify(obj)
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.$message({
                message: '发布成功~',
                type: 'success'
            });
            new_book(obj);
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//保存文章

select_tag();
function select_tag() {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",
        baseURL: '../../api/book.php',
        data: 'data=' + JSON.stringify({ head: 1023, token: (sessionStorage.getItem('token')) })
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.options = res.data.data;
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//获取tag



function upload_img_fun(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    var form1 = document.querySelector('#form2');
    var img_data = (new FormData(form1));
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        loading.close();
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var data = JSON.parse(xmlhttp.responseText);
            if (data.code === '200') {
                app.up_img = '../' + data.src;
                app.$message({
                    message: '上传成功~',
                    type: 'success'
                });
                app.dialogFormVisible = false;
                console.log(data);
            } else {
                app.$message.error(data.msg);
            };
        };
    }
    xmlhttp.open("POST", '../../api/book_upload_img.php', true);
    xmlhttp.send(img_data);
};//上传图片

var testEditor;
window.onload = function () {
    var href = window.location.href;
    var id = '';
    var num = 0;
    if (href.includes('?id=')) {
        id = href.split('?id=')[1];
        select_book(id);
        num = 2000;
    };
    function select_book(obj) {
        var loading = app.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        axios({
            method: "POST",
            baseURL: '../../api/book_set.php',
            data: 'data=' + JSON.stringify({ head: 1052, token: (sessionStorage.getItem('token')), id: obj })
        }).then(function (res) {
            loading.close();
            if (res.data.code === '200') {
                var eid = document.querySelector('#edi');
                eid.innerHTML = res.data.data[0].cont1;
                app.title = res.data.data[0].title;
                app.ids = res.data.data[0].Id;
                app.up_img = res.data.data[0].book_img;
            } else if (res.data.code === '500') {
                window.location.href = res.data.href;
            } else {
                app.$message.error('获取数据失败~');
            };
        }).catch(function (err) {
            console.log(err);
        });
    };


    setTimeout(function () {
        testEditor = editormd("test-editormd", {
            width: "90%",
            height: 600,
            theme: (localStorage.theme) ? localStorage.theme : "dark",
            previewTheme: (localStorage.previewTheme) ? localStorage.previewTheme : "dark",
            editorTheme: (localStorage.editorTheme) ? localStorage.editorTheme : "pastel-on-dark",
            path: './e/lib/',
            onload: function () {
                var eid = document.querySelector('#edi');
                eid.innerText = '获取数据失败了，请刷新手重试。';
            }
        });
    }, 2000);
};
